package pe.edu.upc.dew.grupo5.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo5.model.Pelicula;
import pe.edu.upc.dew.grupo5.service.PeliculaService;

/**
 * Servlet implementation class ComboPeliculaPorCineServlet
 */
public class ComboPeliculaPorCineServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PeliculaService peliculaService;

	@Override
	public void init(ServletConfig config) throws ServletException {
		this.peliculaService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"peliculaService", PeliculaService.class);
	}
	
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String idCine = request.getParameter("idCine");
		List<Pelicula> list = peliculaService.getPeliculaPorCine(Integer.valueOf(idCine));
		try {
			out.printf("<option value='0'>Seleccione Pel&iacute;cula</option>");
			for (Pelicula pelicula: list) {
				out.printf("<option value='%1s'>%2s</option>",
						pelicula.getIdPelicula(), pelicula.getNombre());
			}
		} finally {
			out.close();
		}
	}
	
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

}
